﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using System.Text.RegularExpressions;


using XStudio.Core.Ext.ConvertExtensions;
using XStudio.Core.Log;


namespace Api.Base
{
    public class BaseApi : ApiController
    {
        /// <summary>
        ///   获得指定表单参数的值
        /// </summary>
        /// <param name = "strName">表单参数</param>
        /// <returns>表单参数的值</returns>
        public virtual string GetRequestString(string strName)
        {
            return HttpContext.Current.Request[strName] ?? "";
        }

        /// <summary>
        ///   获得指定表单参数的值
        /// </summary>
        /// <param name = "strName">表单参数</param>
        /// <param name = "defValue">缺省值</param>
        /// <returns>表单参数的值</returns>
        public virtual string GetRequestString(string strName, string defValue)
        {
            return HttpContext.Current.Request[strName] ?? defValue;
        }

        /// <summary>
        ///   获得指定Url参数的int类型值
        /// </summary>
        /// <param name = "strName">Url参数</param>
        /// <param name = "defValue">缺省值</param>
        /// <returns>Url参数的int类型值</returns>
        public virtual int GetRequestInt(string strName, int defValue)
        {
            int result = 0;
            int.TryParse(HttpContext.Current.Request[strName], out result);
            if (result > 0)
            {
                return result;
            }
            return defValue;
        }

        /// <summary>
        /// 获取用户的IP
        /// </summary>
        /// <value>The user IP.</value>
        public virtual string UserIP
        {
            get
            {
                return System.Web.HttpContext.Current.Request.UserHostAddress;
            }
        }

    }
}